package com.sgb.link.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.sgb.link.R;
import com.sgb.link.view.adapter.TeamMainPageTitleAdapter;
import com.sgb.link.view.adapter.YoungAdapterOnItemClick;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author younghow
 * @date 2021/11/15
 * description:标题栏带有tab
 */
public class ActionBarTabLine extends RelativeLayout {

    ImageView iconFontBack;
    TextView fontIconLdrtl;
    private ImageView fivContactSearch;
    private SelectTab mSelectTab;
    private RecyclerView mTabLayout;
    private TeamMainPageTitleAdapter mTeamMainPageTitleAdapter;

    public void setSelectTab(SelectTab clearEdiText) {
        this.mSelectTab = clearEdiText;
    }

    public SelectTab getSelectTab() {
        return mSelectTab;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
    }

    public ActionBarTabLine(Context context) {
        super(context);
        init(context, null);
    }

    public ActionBarTabLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ActionBarTabLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {

            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ActionBarTabYoung, 0, 0);
            try {
                LayoutInflater.from(context).inflate(R.layout.link_module_action_bar_tab_center_line, this);

                //回退按钮
                iconFontBack = findViewById(R.id.iv_back);
                //actionbar的标题
                fivContactSearch = findViewById(R.id.map_view);
                //右边的文字
                fontIconLdrtl = findViewById(R.id.right_view);
                int txtColor = attributes.getColor(R.styleable.ActionBarTabYoung_txtColorTab, 0);

                boolean isVisibleMenu = attributes.getBoolean(R.styleable.ActionBarTabYoung_isVisibleMenuTab, false);
                if (isVisibleMenu) {
                    fivContactSearch.setVisibility(VISIBLE);
                } else {
                    fivContactSearch.setVisibility(GONE);
                }

                String menuFont = attributes.getString(R.styleable.ActionBarTabYoung_menuTextTab);
                if (menuFont == null || "".equals(menuFont)) {
                    menuFont = "保存";
                }
                fontIconLdrtl.setText(menuFont);
                fontIconLdrtl.setTextColor(txtColor);

                boolean isVisibleMenuFunction = attributes.getBoolean(R.styleable.ActionBarTabYoung_isVisibleMenuFunctionTab, false);
                setMenuFunctionVisible(isVisibleMenuFunction);

                RelativeLayout relativeLayout = findViewById(R.id.rl_title);
                View view = findViewById(R.id.status_bar);
                int backgroundColor = attributes.getColor(R.styleable.ActionBarTabYoung_backColorTab, 0);
                if (backgroundColor != 0) {
                    relativeLayout.setBackgroundColor(backgroundColor);
                    view.setBackgroundColor(backgroundColor);
                }

                int tabMaxNumber = attributes.getInt(R.styleable.ActionBarTabYoung_tabMaxNumber, 1);
                mTabLayout = findViewById(R.id.tab_action_bar);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                mTabLayout.setLayoutManager(linearLayoutManager);
                mTeamMainPageTitleAdapter = new TeamMainPageTitleAdapter(getContext(), null);
                mTeamMainPageTitleAdapter.setMostNumber(tabMaxNumber);

                mTeamMainPageTitleAdapter.setOnItemClick(new YoungAdapterOnItemClick<String>() {
                    @Override
                    public void onItemClick(String s, int position) {
                        if (mSelectTab != null) {
                            mSelectTab.select(position,s);
                        }
                    }
                });

                mTeamMainPageTitleAdapter.setItemSelect(true);
                mTeamMainPageTitleAdapter.setDefItem(0);
                mTabLayout.setAdapter(mTeamMainPageTitleAdapter);
            } finally {
                attributes.recycle();
            }
        }
    }

    public void setTitleMenuImg(int originLang) {
        iconFontBack.setImageResource(originLang);
    }

    public void setOnBackClickListener(OnClickListener l) {
        iconFontBack.setOnClickListener(l);
    }

    public void setMenuClick(OnClickListener click) {
        fivContactSearch.setOnClickListener(click);
    }

    public void setMenuIcon(@DrawableRes int drawable) {
        fivContactSearch.setImageResource(drawable);
    }

    public void setMenuVisible(boolean flag) {
        if (flag) {
            fivContactSearch.setVisibility(VISIBLE);
        } else {
            fivContactSearch.setVisibility(GONE);
        }
    }

    public void setMenuFunctionClick(OnClickListener click) {
        fontIconLdrtl.setOnClickListener(click);
    }

    public void setMenuFunctionVisible(boolean isVisibleMenuFunction) {
        if (isVisibleMenuFunction) {
            fontIconLdrtl.setVisibility(VISIBLE);
        } else {
            fontIconLdrtl.setVisibility(GONE);
        }
    }

    public void setMenuFunctionText(String isVisibleMenuFunction) {
        fontIconLdrtl.setText(isVisibleMenuFunction);
    }

    public String getMenuFunctionText() {
        return fontIconLdrtl.getText().toString();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public interface SelectTab {
        default void unSelectTab(TabLayout.Tab tab) {
            MLog.e(tab.getPosition()+"");
        }

        default void selectTab(TabLayout.Tab tab){

        }

        void select(int position,String s);
    }


    public void setData(List<String> list){
        mTeamMainPageTitleAdapter.setDatas(list);
    }

    public void setDataForStr(String...list){
        List<String> list1 = new ArrayList<>(Arrays.asList(list));
        mTeamMainPageTitleAdapter.setDatas(list1);
    }

    public void setSelectPosition(int position){
        mTeamMainPageTitleAdapter.setDefItem(position);
        mTabLayout.scrollToPosition(position);
    }
}
